package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 地区位置Controller
 * 
 * @author ruoyi
 * @date 2024-12-02
 */
@RestController
@RequestMapping("/system/area")
public class AreaController {

    @Autowired
    private DataSource dataSource;

    /**
     * 查询地区位置列表（直接执行 SQL 查询，支持 level 和 parentId 筛选）
     */
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(required = false) Integer level,
            @RequestParam(required = false) Long parentId) {
        // 初始化 JdbcTemplate
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        // 构建 SQL 查询语句
        StringBuilder sql = new StringBuilder("SELECT * FROM area WHERE 1=1");

        // 根据筛选条件动态添加 SQL 条件
        if (level != null) {
            sql.append(" AND level = ").append(level);
        }
        if (parentId != null) {
            sql.append(" AND parentId = ").append(parentId);
        }

        // 执行查询并获取结果
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql.toString());

        // 包装结果
        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setRows(resultList);
        dataTable.setTotal(resultList.size());

        return dataTable;
    }
}
